package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtPackerEngineeringCompanyBinding;
import com.sgb.kjwl.model.entity.packers.EngineeringCompanyEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.adapter.packers.EngineeringCompanyAdapter;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.packers.ThreeLevelPopWindow;
import com.sgb.kjwl.view.widget.packers.TwoLevelPopLinkWindow;
import com.sgb.kjwl.viewmodel.packers.PackersEngineeringCompanyModel;
import com.sgb.kjwl.viewmodel.packers.PackersEngineeringTenderModel;
import com.sgb.kjwl.viewmodel.packers.PackersFindPeopleModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.common.base.BaseApplication;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liwei
 * @date 2021/3/24
 * description:工程公司列表
 */
public class PackersEngineeringCompanyActivity extends AppActivity implements ThreeLevelPopWindow.SelectResultClick<AddressEntity> {

    public PackersEngineeringCompanyModel viewModel;
    private AtPackerEngineeringCompanyBinding binding;
    private EngineeringCompanyAdapter adapter;

    /**
     * 团队规模
     */
    private PickPopWindow1 popTeamSize;
    private PickResultEntity pickResultTeamSize;
    /**
     * 所在区域
     */
    private ThreeLevelAreaWindow popCity;
    private PickResultEntity pickResultProvince;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;
    private AddressEntity mAddress;
    private TwoLevelPopLinkWindow<AddressEntity> mTwoLevelPopWindow;
    private String mQualificationTypeNo;
    private String mQualificationClassNo;
    private String mQualificationClassName;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, PackersEngineeringCompanyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getNewList("", "", "", "全部".equals(pickResultTeamSize.getName()) ? "" : pickResultTeamSize.getName(),
                pickResultProvince.getNo(), pickResultCity.getNo(), pickResultArea.getNo(), isLoadMore, mQualificationTypeNo, mQualificationClassName);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_packer_engineering_company);
        if (viewModel == null){
            viewModel = new ViewModelProvider(this).get(PackersEngineeringCompanyModel.class);
        }

        mQualificationClassNo = getIntent().getStringExtra("mQualificationClassNo");
        mQualificationClassName = getIntent().getStringExtra("mQualificationClassName");

        if (adapter == null) {
            adapter = new EngineeringCompanyAdapter(getActivity(), null, Glide.with(this));
        }

        adapter.setOnItemClick(new AdapterOnItemClick<EngineeringCompanyEntity>() {
            @Override
            public void onItemClick(EngineeringCompanyEntity entity, int position) {
                if (TextUtils.isEmpty(entity.getId())) {
                    ToastUtil.show(getActivity(), "企业店铺id不能为空");
                    return;
                }
                WebsiteInfoActivity.start(getActivity(), entity.getCompanyNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY);
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp5)));
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });

        pickResultTeamSize = new PickResultEntity();
        pickResultProvince = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();

        mAddress = (AddressEntity) getIntent().getSerializableExtra("entity");
        if (mAddress != null) {
            mQualificationTypeNo = mAddress.getNo();
        }

        mTwoLevelPopWindow = new TwoLevelPopLinkWindow<>(this);
        mTwoLevelPopWindow.setSelectResultClick(this);
        mTwoLevelPopWindow.setBgView(binding.grayLayout);
        mTwoLevelPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });

        mTwoLevelPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {

            }
        });
        viewModel.getIntelligenceType();


    }

    @Override
    protected void init() {
        initFilterData(false);
    }

    @Override
    public void onMultiClick(View v) {
        super.onMultiClick(v);
        int id = v.getId();
        if (id == R.id.tv_team_size) {
            if (popTeamSize == null) {
                viewModel.getTeamSize(0);
            } else {
                showTeamSizePopWindow(null);
            }
        } else if (id == R.id.tv_area) {
            if (popCity == null) {
                viewModel.getCityList();
            } else {
                showCityPop();
            }
        }
    }

    public void showList(List<EngineeringCompanyEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(list == null || list.size() < 10);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
                binding.recyclerView.smoothScrollToPosition(0);
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 团队规模pop
     *
     * @param data 团队规模popWindow的数据
     */
    public void showTeamSizePopWindow(List<PickSelectEntity> data) {
        if (popTeamSize == null) {
            popTeamSize = new PickPopWindow1(this);
            popTeamSize.setBgView(binding.grayLayout);
            popTeamSize.setOnResultClick((PickPopWindow1.OnResultClick) (key1, key2, key3) -> {
                pickResultTeamSize.setNo((String) key1);
                pickResultTeamSize.setName((String) key2);
                pickResultTeamSize.setPosition((int) key3);

                initFilterData(false);
            });
            popTeamSize.setOnDismissListener(() -> {

            });

            popTeamSize.setData(data);
        }

    }

    /**
     * 省市区pop
     *
     */
    public void showCityPop() {
        if (popCity == null) {
            popCity = new ThreeLevelAreaWindow(getActivity()) {
                @Override
                protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.provinceClick(baseAreaEntity, position);
                    pickResultProvince.setNo(baseAreaEntity.getNo());
                    pickResultProvince.setName(baseAreaEntity.getSimpleName());
                    if ("全部".equals(baseAreaEntity.getSimpleName()) || pickResultProvince.getName().equals(baseAreaEntity.getSimpleName())) {
                        pickResultCity.setNo("");
                        pickResultCity.setName("");
                    }
                }

                @Override
                protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.cityClick(baseAreaEntity, position);
                    pickResultCity.setNo(baseAreaEntity.getNo());
                    pickResultCity.setName(baseAreaEntity.getSimpleName());
                    if ("全部".equals(baseAreaEntity.getSimpleName()) || pickResultCity.getName().equals(baseAreaEntity.getSimpleName())) {
                        pickResultArea.setNo("");
                        pickResultArea.setName("");
                    }
                }

                @Override
                protected void areaClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.areaClick(baseAreaEntity, position);
                    pickResultArea.setNo(baseAreaEntity.getNo());
                    pickResultArea.setName(baseAreaEntity.getSimpleName());
                }
            };
            popCity.setBgView(binding.grayLayout);
            popCity.setProvinceSingle();
            popCity.setCitySingle();
            popCity.setAreaSingle();
            popCity.setShowSimpleName(true);
            popCity.setOnResultClick((key1, key2, key3) -> {
                initFilterData(false);
            });
            popCity.setOnDismissListener(() -> {
            });
        }
    }

    public void updateType(List<AddressEntity<List<AddressEntity>>> dataType) {
        List<AddressEntity> oneLevel = new ArrayList<>();
        List<List<AddressEntity>> twoLevel = new ArrayList<>();
        oneLevel.addAll(dataType);
        for (AddressEntity<List<AddressEntity>> listAddressEntity : dataType) {
            List<AddressEntity> list = listAddressEntity.getChildren();
            if (list != null) {
                twoLevel.add(list);
            } else {
                twoLevel.add(new ArrayList<>());
            }
        }

        int position = 0;
        if (mAddress != null) {
            for (int i = 0; i < oneLevel.size(); i++) {
                if (mAddress.getNo().equals(oneLevel.get(i).getNo())) {
                    position = i;
                    break;
                }
            }
            mTwoLevelPopWindow.setProvince(mAddress);
        }else {
            mTwoLevelPopWindow.setProvince(new AddressEntity("0","全部",""));
        }

        mTwoLevelPopWindow.setTwoLevelData(oneLevel,twoLevel,position);
        mTwoLevelPopWindow.setProvinceDefaultItem(position);
        if (!TextUtils.isEmpty(mQualificationClassName)){
            mTwoLevelPopWindow.setName(mQualificationClassName,mQualificationClassNo);
        }else {
            mTwoLevelPopWindow.setCity(new AddressEntity("0","全部",""));
        }

        if (!TextUtils.isEmpty(mQualificationClassName)) {
            mTwoLevelPopWindow.setCityDefaultItemForNo(mQualificationClassName);
        }
    }

    @Override
    public void result(List<AddressEntity> pickSelectEntitiesProvince,List<AddressEntity> pickSelectEntitiesCity,List<AddressEntity> pickSelectEntitiesArea) {
        if (pickSelectEntitiesProvince == null) {
            return;
        }

        if (pickSelectEntitiesCity == null){
            return;
        }

        if (pickSelectEntitiesProvince.size() > 0 && pickSelectEntitiesCity.size() > 0) {
            mQualificationTypeNo = "全部".equals(pickSelectEntitiesProvince.get(0).getName()) ? "" : pickSelectEntitiesProvince.get(0).getNo();
            mQualificationClassName = "全部".equals(pickSelectEntitiesCity.get(0).getName())?"":pickSelectEntitiesCity.get(0).getName();
            initFilterData(false);
            return;
        }

        if (pickSelectEntitiesProvince.size() > 0) {
            mQualificationTypeNo = "全部".equals(pickSelectEntitiesProvince.get(0).getName()) ? "" : pickSelectEntitiesProvince.get(0).getNo();
            mQualificationClassName = "";
            initFilterData(false);
            return;
        }

        if (pickSelectEntitiesCity.size() > 0) {
            mQualificationTypeNo = "";
            mQualificationClassName = "全部".equals(pickSelectEntitiesCity.get(1).getName())?"":pickSelectEntitiesCity.get(1).getName();
            initFilterData(false);
        }
    }

    @Override
    public void reset() {
        mQualificationTypeNo = "";
        mQualificationClassName = "";
        initFilterData(false);
        mTwoLevelPopWindow.clear();
        mTwoLevelPopWindow.dismiss();
    }

    @Override
    public void onBackPressed() {
        if (!mTwoLevelPopWindow.isShowing()){
            super.onBackPressed();
        }
    }
}
